package com.meiqiongproperty.yiguanjia.fragment.home.tenement;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.UtilitiesLayoutBinding;
import com.meiqiongproperty.yiguanjia.db.utils.DBUserUtils;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.pay.PayTypeFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.SimpleHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendChargeProperty;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.pay.CommPayWX;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.pay.CommPayZFB;
import com.meiqiongproperty.yiguanjia.ui.dialog.FragmentExchangeController;
import com.meiqiongproperty.yiguanjia.ui.flowlayout.FlowLayout;
import com.meiqiongproperty.yiguanjia.ui.flowlayout.TagAdapter;
import com.meiqiongproperty.yiguanjia.ui.flowlayout.TagFlowLayout;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

/**
 * 公共事业费
 *
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment.home.tenement
 * @User ChenJing
 * @Date 2017/5/4
 */

public class UtilitiesFragment extends BaseFragment   {
    UtilitiesLayoutBinding binding;
    private String[] times = new String[]{"3个月", "6个月", "12个月"};
    int type,mouth;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.utilities_layout, container, false);
        binding.setClick(this);
        initView();
        return binding.getRoot();
    }

    private void initView() {
        binding.navigation.setTitle("公共事业费");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        binding.address.setText(DBUserUtils.getAddress(getActivity()));
        binding.RoomArea.setText(DBUserUtils.getmDefaultHouse(getActivity()).getRoomArea()+"㎡");
        binding.RoomPrice.setText(DBUserUtils.getmDefaultHouse(getActivity()).getRoomPrice()+"元/㎡");

        final LayoutInflater mInflater = LayoutInflater.from(getActivity());

        binding.publicTimeFlow.setAdapter(new TagAdapter<String>(times) {
            @Override
            public View getView(FlowLayout parent, int position, String s) {
                TextView tv=(TextView)mInflater.inflate(R.layout.public_time_item,binding.publicTimeFlow,false);
                tv.setText(s);
                return tv;
            }
        });
        binding.publicTimeFlow.getAdapter().setSelectedList(0);
        binding.amount.setText(Utils.toString(Utils.toDouble(DBUserUtils.getmDefaultHouse(getActivity()).getRoomPrice())*3));
        binding.publicTimeFlow.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                switch (position){
                    case 0:
                        mouth=3;
                        break;
                    case 1:
                        mouth=6;
                        break;
                    case 2:
                        mouth=12;
                        break;
                }
                LogUtil.d("DBUserUtils="+DBUserUtils.getmDefaultHouse(getActivity()).toString());
                binding.amount.setText(Utils.toString((Utils.toDouble(DBUserUtils.getmDefaultHouse(getActivity()).getRoomPrice())*Utils.toDouble(DBUserUtils.getmDefaultHouse(getActivity()).getRoomArea()))*mouth)+"元");
                return true;
            }
        });
    }

    public void commitPay(View view) {
        SendChargePropertyMethod();
    }

    private void SendChargePropertyMethod() {
        SendChargeProperty sendChargeProperty=new SendChargeProperty();
        sendChargeProperty.setPayMethod(type);
        sendChargeProperty.setToken(DBUserUtils.getUserInfo(getActivity()).getToken());
        sendChargeProperty.setRoomID(DBUserUtils.getmDefaultHouse(getActivity()).getID());
        switch (type){
            case 1:
                ApiService.chargeProperty(getActivity(), new SimpleHttpCallback<CommPayZFB>() {
                    @Override
                    public void onNext(CommPayZFB commPayZFB) {
                         String orderInfo = commPayZFB.getParameter().getParameter();   // 订单信息
                        doAlipay(orderInfo);
                    }
                }, Utils.getRequestBody(sendChargeProperty));
                break;
            case 2:
                ApiService.chargePropertyWX(getActivity(), new SimpleHttpCallback<CommPayWX>() {
                    @Override
                    public void onNext(CommPayWX commPayWX) {
                        doWXPay(commPayWX.getParameter().getParameter(),commPayWX.getParameter().getParameter().getAppid());
                    }
                }, Utils.getRequestBody(sendChargeProperty));
                break;
        }

    }

    public void select_pay_type(View view){
        PayTypeFragment payTypeSeleor = new PayTypeFragment();
        payTypeSeleor.setPayTypeClickListener(new PayTypeFragment.PayTypeClickListener() {
            @Override
            public void setData(Object data) {
                type= (int) data;
                switch (type){
                    case 1:
                        binding.type.setText("支付宝支付");
                        break;
                    case 2:
                        binding.type.setText("微信支付");
                        break;
                }
            }
        });
        FragmentExchangeController.addFragment(getFragmentManager(), payTypeSeleor, payTypeSeleor.getTagName());
    }

}
